var MgrUser = {
    table: null,
    layerIndex: -1
}

MgrUser.init = function () {
    layui.form.render();
    var me = this;
    this.table = new LayUITable("table", '/system/sysUser/list', this.initColumns());
    me.table.params = {
        height: $(".dawn-content").height() - 50
    }
    me.table.setQueryParams(me.queryParams);
    me.table.handleListenToolBar = function (event, checkStatus, obj) {
        switch (event) {
            case "add":
                layui.laytpl($("#info").html()).render({password: true}, function (html) {
                    MgrUser.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '添加用户',
                        area: ['800px', '100%'], //宽高
                        content: html,
                        success: function () {
                            MgrUser.add();
                        }
                    });
                });
                break;
        }
    };
    me.table.handleListenTool = function (event, data, obj) {
        switch (event) {
            case "edit":
                layui.laytpl($("#info").html()).render({password: false}, function (html) {
                    MgrUser.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '编辑用户',
                        area: ['800px', '100%'], //宽高
                        content: html,
                        success: function () {
                            MgrUser.update(data.id);
                        }
                    });
                });
                break;
            case "delete":
                var operation = function () {
                    $request.get("/system/sysUser/delete", function (res) {
                        if(res.success) {
                            Dawn.success("删除成功!");
                            MgrUser.table.init();
                        } else {
                            Dawn.error(res.message);
                        }
                    }).set("userId", data.id).start();
                };
                Dawn.deleteConfirm("是否删除用户" + data.account + "?", operation);
                break;
            case "refresh":
                var index = layer.confirm('是否重置密码为111111？', {
                    btn: ['确定', '取消'],
                    shade: false,
                    title: "重置密码"
                }, function () {
                    layer.close(index);
                    $request.get("/system/sysUser/reset", function (data) {
                        Dawn.success("重置密码成功!");
                    }, function (data) {
                        Dawn.error("重置密码失败!");
                    }).set("userId", data.id).start();
                });
                break;
            case "freeze":
                $request.get("/system/sysUser/freeze", function (data) {
                    if(data.success) {
                        Dawn.success("冻结成功!");
                        MgrUser.table.refresh();
                    } else {
                        Dawn.error(data.message);
                    }
                }).set("userId", data.id).start();
                break;
            case "unfreeze":
                $request.get("/system/sysUser/unfreeze", function (data) {
                    if(data.success) {
                        Dawn.success("解除冻结成功!");
                        MgrUser.table.refresh();
                    } else {
                        Dawn.error(data.message);
                    }
                }).set("userId", data.id).start();
                break;
            case "setRole":
                layui.laytpl($("#assign").html()).render({}, function (html) {
                    MgrUser.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '角色分配',
                        area: ['300px', '100%'], //宽高
                        content: html,
                        success: function () {
                            MgrUser.assign(data.id);
                        }
                    });
                });
                break
        }
    };
    me.table.init();
    this.addEvents();
};

MgrUser.addEvents = function () {
    var me = this;
    $("#search").click(function () {
        me.search();
    })
    $("#reset").click(function () {
        me.reset();
    });
}

MgrUser.update = function(id) {
    $request.get("/system/sysUser/user_info", function(data) {
        if(data.success) {
            layui.form.val("info", data.data.user);
            layui.laydate.render({
                elem: "#birthday",
                value: data.data.user.birthday
            });
            layui.form.render();
            layui.form.on('submit(save)', function (data) {
                $request.postJson("/system/sysUser/edit", function (res) {
                    if (res.success) {
                        Dawn.success("更新成功!");
                        MgrUser.table.refresh();
                        layui.layer.close(MgrUser.itemIndex);
                    } else {
                        Dawn.error(res.message);
                    }
                }).setData(data.field).start();
                return false;
            });
        } else {
            Dawn.error(data.message);
        }
    }).setData({
        userId: id
    }).setAsync(false).start();
};

MgrUser.add = function() {
    layui.laydate.render({
        elem: "#birthday"
    });
    layui.form.verify({
        rePassword: function(value, item){
            let pwd = $("input[name='password']").val();
            if (pwd != value) {
                return '两次密码输入不一致';
            }
        }
    });
    layui.form.render();
    layui.form.on('submit(save)', function (data) {
        $request.postJson("/system/sysUser/add", function (res) {
            if (res.success) {
                Dawn.success("保存成功!");
                MgrUser.table.refresh();
                layui.layer.close(MgrUser.itemIndex);
            } else {
                Dawn.error(res.message);
            }
        }).setData(data.field).start();
        return false;
    });
};

MgrUser.assign = function(id) {
    $("#btn_close").unbind().bind("click", function () {
        layui.layer.close(MgrUser.itemIndex);
    });
    $("#btn_save").unbind().bind("click", function () {
        var ids = Dawn.zTreeCheckedNodes("zTree");
        $request.postJson("/system/sysUser/setRole", function (data) {
            if (data.success) {
                Dawn.success("分配角色成功!");
                MgrUser.table.refresh();
                layui.layer.close(MgrUser.itemIndex);
            } else {
                Dawn.error(data.message);
            }
        }).set("roleIds", ids).set("userId", id).start();
    });
    var setting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    var ztree = new $ZTree("zTree", "/system/sysRole/roleTreeListByUserId?userId=" + id);
    ztree.setSettings(setting);
    ztree.init();
};

MgrUser.reset = function () {
    $("#name").val("");
    $("#time").val("");
    $("#status").val("");
    layui.form.render();
    this.search();
}

MgrUser.search = function () {
    this.table.init();
}

MgrUser.queryParams = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['status'] = $("#status").val();
    var time = $("#time").val();
    if (time != "") {
        var args = time.split(" - ");
        queryData['beginTime'] = $.trim(args[0]);
        queryData['endTime'] = $.trim(args[1]);
    }
    return queryData;
}

MgrUser.initColumns = function () {
    return [
        [
            {
                field: 'id',
                title: 'ID',
                align: 'center',
                hide: true
            },
            {
                field: 'account',
                title: '账号',
                align: 'center',
                sort: true
            },
            {
                field: 'name',
                title: '姓名',
                align: 'center',
                sort: true
            }, {
            field: 'sex',
            title: '性别',
            align: 'center',
            templet: function (d) {
                return d.sexName;
            },
            hide: true
        }, {
            field: 'roleName',
            title: '角色',
            align: 'center',
            hide: true
        }, {
            field: 'email',
            title: '邮箱',
            align: 'center',
            hide: true
        }, {
            field: 'phone',
            title: '电话',
            align: 'center'
        }, {
            field: 'status',
            title: '状态',
            align: 'center',
            sort: true,
            width: 90,
            templet: function (d) {
                return d.statusName;
            }
        }, {
            field: 'createtime',
            title: '注册时间',
            align: 'center',
            sort: true
        }, {
            fixed: 'right',
            title: '操作',
            width: 340,
            toolbar: '#tableTool',
            align: 'center'
        }
        ]
    ];
}

$(function () {
    layui.element.init();
    MgrUser.init();
});
